package controller;

import controller.exceptions.InvalidOperationException;
import controller.exceptions.InvalidVersionException;
import controller.exceptions.ItemAlreadyRemovedException;
import filesystem.exceptions.InvalidModelItemException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.*;

/**
 * Conroller
 */
public class Controller {
    
    public enum operation {

        ADD,
        EDIT,
        REMOVE
    }

    /**
     * Execute transaction on the model
     * @param o type of transaction (ADD, EDIT, REMOVE)
     * @param model model
     * @param item changed object of the model
     */
    public void execute(operation o, Model model, ModelItem item) throws InvalidModelItemException, IOException {
        switch (o) {
            case ADD:
                model.addItem(item);
                break;
            case EDIT:
        try {
            model.setItem(item);
        } catch (InvalidVersionException ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ItemAlreadyRemovedException ex) {
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
                break;
            case REMOVE:
                model.removeItem(item);
                break;
            default:
                //throw new InvalidOperationException();
        }
    }

    /**
    * Select objects from the File System by parametr
    * @param model model
    * @param parametr parametr of the search
    * @return List of the found objects
    */
    public ArrayList selectItem(Model model, String parametr) throws IOException {
        return model.selectItems();
    }
}
